package com.apes.scm.settle.payment.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.finance.model.IncomeType;
import com.apes.scm.masterdata.frame.model.Dept;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SupplierIncomePayItem.CONST_TABLE_NAME)
public class SupplierIncomePayItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "Supplier_Income_Pay_Item";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "综合付款明细表", prefix = "FI")
    @Comment("付款明细编码")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "supplierIncomePay", referencedColumnName = "id")
    @Comment("付款单编码")
    private SupplierIncomePay supplierIncomePay; /*付款主表*/

    @Column(name = "income_Id", length = 32)
    @Comment("供应商收入单号")
    private String incomeId;  // 供应商收入单明细

    @Column(name = "income_Item_Id", length = 32)
    @Comment("供应商收入单明细")
    private String incomeItemId;  // 供应商收入单明细

    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("本次扣款金额")
    private double amount = 0; //本次扣款金额

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "income_category", referencedColumnName = "id")
    @Comment("收入类别")
    private Enumeration inComeCategory;



    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "income_type_id", referencedColumnName = "id")
    @Comment("收入类型")
    private IncomeType incomeType;  // 收入类型

    @Column(name = "income_Amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("收入金额")
    private double incomeAmount = 0; // 收入金额

    @Column(name = "recovery_Amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("已扣款金额")
    private double recoveryAmount = 0; // 已扣款金额

    @Column(name = "freeze_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("冻结金额")
    private double freezeAmount = 0;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "start_date")
    @Comment("业务期起")
    private Date startDate; //业务开始日期

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "end_date")
    @Comment("业务期止")
    private Date endDate; //业务结束日期

    @Transient
    private double needIncomeAmount = 0; //付款金额

    public double getNeedIncomeAmount(){
        return this.incomeAmount - this.recoveryAmount - this.freezeAmount;
    }
}
